﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lilium.Web.Mvc;
using Mvc2.Models;
using Lilium.Web.Mvc.EntityFilters;
using Mvc2.Services;
using Lilium;

namespace Mvc2.Controllers
{
    public sealed class Test : MarshalByRefObject
    {

    }

    [HandleError]
    public class HomeController : EntityController
    {
        private static readonly IQueryable<DataRecord> Data = CreateRandomData().Take(10010).ToArray().AsQueryable();

        public HomeController(Lilium.IServiceProvider serviceProvider)
            : base(serviceProvider)
        {
        }

        [EntityFilter(typeof(EvenOddFilter), Order = 0)]
        [Alphabet(typeof(DataRecord), "Field1")]
        [Pager(typeof(DataRecord), Order = 2)]
        public ActionResult Index()
        {
            ViewData["Message"] = "Welcome to ASP.NET MVC!";

            return View(this.ApplyFilters(Data));
        }

        public ActionResult ExportToExcel()
        {
            return View(this.ApplyFilters(Data).ToArray());
        }

        public ActionResult About()
        {
            return View();
        }

        private static IEnumerable<DataRecord> CreateRandomData()
        {
            var counter = 0;
            while (true)
                yield return new DataRecord()
                {
                    Id = counter++,
                    Field1 = Guid.NewGuid().ToString(),
                    Field2 = Guid.NewGuid().ToString(),
                    Field3 = Guid.NewGuid().ToString()
                };
        }
    }
}
